<template>
  <div class="content">
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <span>个人信息</span>
        <el-button style="float: right; padding: 3px 0" type="primary" @click="editUser()">编辑</el-button>
        <el-button style="float: right; padding: 3px 0; margin-right:10px" type="primary" @click="editPwd()">修改密码</el-button>
      </div>
      <el-form label-width="80px">
        <el-form-item label="用户名:">{{user.username}}</el-form-item>
        <el-form-item label="角色:">{{user.role}}</el-form-item>
        <el-form-item label="手机号:">{{user.phone}}</el-form-item>
        <el-form-item label="姓名:">{{user.name}}</el-form-item>
      </el-form>
    </el-card>
    <el-dialog title="编辑" :visible.sync="editshow" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="用户名:" prop="username">
          <el-input size="small" v-model="editForm.username" placeholder="输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input size="small" v-model="editForm.phone" placeholder="输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input size="small" v-model="editForm.name" placeholder="输入姓名"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary"  class="title" @click="userSave('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <!-- 修改密码 -->
    <el-dialog title="修改密码" :visible.sync="editpwdshow" width="30%" @click='closeDialog("pwd")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="pwdRules">
        <el-form-item label="旧密码:">
          <el-input size="small" v-model="editForm.oldPwd" placeholder="输入旧密码" type="password"></el-input>
        </el-form-item>
        <el-form-item label="新密码:" prop="password">
          <el-input size="small" v-model="editForm.password" placeholder="输入新密码" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("pwd")'>取消</el-button>
        <el-button size="small" type="primary" class="title" @click="pwdSave('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  PersonalSave,
  PersonalUpdatePwd
} from '../../api/userMG'
export default {
  
  data() {
    let validPassword=(rule,value,callback)=>{
        let reg= /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$/
        if(!reg.test(value)){callback(new Error('密码必须是由4-20位字母+数字组合'))
        }else{
            callback()
        }
    };
    return {
      editshow: false, //控制编辑页面显示与隐藏
      editpwdshow: false, //控制修改密码
      user: "",
      // 编辑与添加
      editForm: {
        id: "",
        username: "",
        name: "",
        phone: "",
        password: "",
        oldPwd:""
      },
       rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
            required: true,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ]
      },
      pwdRules:{
         password: [{ required: true, message: '请输入密码', trigger: 'blur' },{ validator: validPassword, trigger: 'blur' }]
      }
    };
  },
  created() {
    this.user = JSON.parse(localStorage.getItem("userdata"));
  },
  methods:{
    // 关闭编辑、增加弹出框
    getUserInfo(){
      this.user = JSON.parse(localStorage.getItem("userdata"));
    },
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editshow = false
      } else if (dialog == 'pwd') {
        this.editpwdshow = false
      }
    },
    editUser(){
      this.editshow = true
      this.editForm.id = this.user.id
      this.editForm.username = this.user.username
      this.editForm.name = this.user.name
      this.editForm.phone = this.user.phone
    },
    editPwd(){
      this.editpwdshow =true
      this.editForm.id = this.user.id
      this.editForm.username = this.user.username
      this.editForm.password=''
      this.editForm.oldPwd=''
    },
    //修改信息
    userSave(editData){
      var that = this
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          PersonalSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              if (res.success) {
                this.editshow = false
                localStorage.setItem('userdata', JSON.stringify(res.data))
                that.getUserInfo()
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    //修改密码
    pwdSave(editData){
      this.$refs[editData].validate(valid => {
        if (valid) {
          PersonalUpdatePwd(this.editForm)
            .then(res => {
              this.editFormVisible = false
              console.log(res);
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                })
                this.editpwdshow =false
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    }
  }
};
</script>

<style>
.content {
  margin-left: 600px;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  width: 300px;
}
</style>